package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * This class represents bank's client
 * 
 * @author Anton Kotov
 * 
 */
@SuppressWarnings("serial")
public class Client implements Serializable {

    public Client(String name, Gender gender, Account... accounts) {
	this.name = name;
	this.gender = gender;
	for (Account account : accounts) {
	    if (account == null) {
		throw new IllegalArgumentException();
	    }
	    this.accounts.add(account);
	}
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public Gender getGender() {
	return gender;
    }

    public void setGender(Gender gender) {
	this.gender = gender;
    }

    public Set<Account> getAccounts() {
	return Collections.unmodifiableSet(accounts);
    }

    public Client addAccount(Account account) {
	if (account == null) {
	    throw new IllegalArgumentException();
	}
	accounts.add(account);

	return this;
    }

    public String getClientSalutation() {
	return gender + " " + name;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (!(obj instanceof Client)) {
	    return false;
	}
	Client other = (Client) obj;
	if (name == null) {
	    if (other.name != null) {
		return false;
	    }
	} else if (!name.equals(other.name)) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	StringBuffer buffer = new StringBuffer("Client [name=" + name
		+ ", gender=" + gender + ", accounts=");
	for (Account account : accounts) {
	    buffer.append("\n\t");
	    buffer.append(account);
	}
	buffer.append("\n\t]");
	return buffer.toString();
    }

    private String name;
    private Gender gender;

    private Set<Account> accounts = new HashSet<>();
}
